const {exec, escape} = require('../db/mysql')

// 获取所有车辆（未注销）
const getCarList = async () => {
  const sql = `select * from car;`
  return await exec(sql)
}
// 添加一台车辆
const newCar = async ({car_number, logon_date, valid_date, insurance_date}) => {
  car_number = escape(car_number)
  logon_date = escape(logon_date)
  valid_date = escape(valid_date)
  insurance_date = escape(insurance_date)
  const sql = `insert into car(car_number, logon_date, valid_date, insurance_date, count, isuse, isdel)
               values (${car_number}, ${logon_date}, ${valid_date}, ${insurance_date}, 0, 0, 0)`
  return await exec(sql).then(data => {
    if (data.affectedRows > 0) {
      return true
    } else {
      return false
    }
  })
}
// 修改车辆
const updateCar = async ({car_number, insurance_date, scrap_date, appoint_date, count, isuse}) => {
  car_number = escape(car_number)
  insurance_date = escape(insurance_date)
  scrap_date = escape(scrap_date)
  appoint_date = escape(appoint_date)
  count = escape(count)
  isuse = escape(isuse)
  // 1. insurance_date, scarp_date
  // 2. appoint_date, count, isuse
  let sql = `update car set `
  if (insurance_date !== 'NULL') {
    sql += `insurance_date = ${insurance_date}`
  }
  if (scrap_date !== 'NULL') {
    sql += `, scrap_date = ${scrap_date}`
  }
  if (appoint_date !== 'NULL') {
    sql += `appoint_date = ${appoint_date},
            count = ${count},
            isuse = ${isuse}`
  }
  sql += ` where car_number = ${car_number}`
  return await exec(sql).then(data => {
    if (data.affectedRows > 0) {
      return true
    } else {
      return false
    }
  })
}
// 取消预约车辆(取消分配)
const cancelUpdateCar = async ({car_number, count, isuse}) => {
  car_number = escape(car_number)
  count = escape(count)
  isuse = escape(isuse)
  let sql = `update car set isuse = ${isuse}, appoint_date = NULL `
  if (count !== 'NULL') {
    sql += `, count = ${count}`
  }
  sql += ` where car_number = ${car_number}`
  return await exec(sql).then(data => {
    if (data.affectedRows > 0) {
      return true
    } else {
      return false
    }
  })
}
// 删除车辆，伪删除
const delCar = async (id) => {
  id = escape(id)
  const sql = `update car
               set isdel = 1
               where id = ${id}`
  return await exec(sql).then(data => {
    if (data.affectedRows > 0) {
      return true
    } else {
      return false
    }
  })
}
// 重置车辆预约时间
const resetCar = async (arr) => {
  arr.forEach(item => {
    item = escape(item)
  })
  let sql = `update car set appoint_date = NULL, isuse = 0 where `
  for (let i = 0; i < arr.length; i++) {
    sql += `id = ${arr[i]} `
    if (i !== arr.length - 1) {
      sql += ' or '
    }
  }
  return await exec(sql).then(data => {
    if (data.affectedRows > 0) {
      return true
    } else {
      return false
    }
  })
}


// 获取车辆详细信息
const getCarDetail = async (car_number) => {
  car_number = escape(car_number)
  const sql = `select *
               from car
               where car_number = ${car_number}`
  return await exec(sql).then(rows => {
    return rows[0] || {}
  })
}
// 获取所有未预约车辆（未注销）
const getNoReversedCar = async () => {
  const sql = `select *
               from car
               where isuse = 0
                 and isdel = 0;`
  return await exec(sql)
}

module.exports = {
  getCarList,
  newCar,
  updateCar,
  delCar,
  cancelUpdateCar,
  getCarDetail,
  getNoReversedCar,
  resetCar
}
